package com.springcloud.wujie_study_day01_eureka_consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Classname TestController
 * @Description TODO
 * @Date 2019/5/22 15:02
 * @Created by wujie
 */
@RestController
public class TestController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

     @RequestMapping(value = "consumer")
      public String consumer(){
         ServiceInstance serviceInstance = loadBalancerClient.choose("wujie-study-day01-eureka-client");
         String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/test";
         System.out.println(url);
         return restTemplate.getForObject(url,String.class);
      }
}
